import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: RadioPage(),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class RadioPage extends StatefulWidget {
  RadioPage({Key? key}) : super(key: key);

  @override
  _RadioState createState() => _RadioState();
}

class _RadioState extends State<RadioPage> {
  bool flag = true;
  int sex = 1;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Radio")),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: [
            Row(
              children: [
                Text("female"),
                Radio(
                    //Radio标识，获取后可以知道是哪个radio
                    value: 1,
                    //radio是单选所以同组的只有一个可以选中。
                    groupValue: this.sex,
                    onChanged: (v) {
                      setState(() {
                        this.sex = v as int;
                      });
                    }),
                Text("male"),
                Radio(
                    value: 2,
                    groupValue: this.sex,
                    onChanged: (v) {
                      setState(() {
                        this.sex = v as int;
                      });
                    }),
              ],
            ),
            Row(children: [
              Text("${this.sex} "),
              Text(this.sex == 1 ? "female" : "male")
            ]),
            SizedBox(height: 30),
            Divider(),
            Text("Tadio List Tile Sample"),
            SizedBox(height: 30),
            RadioListTile(
                value: 1,
                groupValue: this.sex,
                //secondary也可以通过Image配置
                secondary: Icon(Icons.male),
                title: Text("男"),
                subtitle: Text("男 描述"),
                selected: this.sex == 1,
                onChanged: (v) {
                  setState(() {
                    this.sex = v as int;
                  });
                }),
            RadioListTile(
                value: 2,
                title: Text("女"),
                subtitle: Text("女 描述"),
                secondary: Icon(Icons.female),
                groupValue: this.sex,
                selected: this.sex == 2,
                onChanged: (v) {
                  setState(() {
                    this.sex = v as int;
                  });
                }),
            SizedBox(height: 20),
            Switch(
                value: this.flag,
                onChanged: (v) {
                  setState(() {
                    print(v);
                    this.flag = v;
                  });
                })
          ],
        ),
      ),
    );
  }
}
